﻿@model ByHand.Model.Provider
@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var result = ViewBag.Result;
}
<div id="provider-error">
</div>
@Html.ActionLink("Return", "Index", "Provider", new { }, new { Class = "btn btn-primary" })
@using (Html.BeginForm("Save", "Provider", FormMethod.Post, new { id = "provider-form", Class = "form-horizotal", onsubmit = "return validform();" }))
{
    <input type="hidden" name="id" value="@Model.ID" />
    <input type="text" name="name" value="@Model.Name"  placeholder="Name" class="form-control" style="width:180px;"/>
    <input type="text" name="bgcolor" value="@Model.BgColor" placeholder="background color code" class="form-control" style="width:180px;" />
    <input type="text" name="linkcolor" value="@Model.LinkColor" placeholder="link color code" class="form-control" style="width:180px;" />
    if (Model.ID == 0)
    {
    <input type="text" name="code" value="@Model.ProviderCode" placeholder="Code" class="form-control" style="width:180px;"/>
    }
    <input type="submit" value="Save" class="btn btn-success" />
}
<h3>Upload Logo</h3>
@{if (Model.ID != 0) { Html.RenderAction("Index", "Upload", new { id = Model.ID, type = 2 }); }}
<h3>Upload Background</h3>
@{if (Model.ID != 0) { Html.RenderAction("Index", "Upload", new { id = Model.ID, type = 4 }); }}
@if (Model.ID != 0)
{ 
    <div style="margin-left: 20px">
        <h3>Merchant</h3>
        @if (TempData["mess"] != null)
        {
            <div class="alert alert-success">
                <p>@TempData["mess"]</p>
            </div>}
        @using (Html.BeginForm("Merchant", "Provider", FormMethod.Post, new { id = "form-merchant" }))
        {
            <div class="input-prepend">
                <span class="add-on">ID</span>
                <input type="hidden" value="@Model.ID" name="providerID" />
                <input class="span2" name="merchantID" type="text" value="@Model.MerchantID" placeholder="Merchant ID">
            </div>
            <button type="submit" class="btn btn-primary">Save changes</button>
        }
    </div>
}
<script>
    var id = '@Model.ID';
    function displayerror(errortext) {
        var alerthtml = '<div class="alert fade in">' +
                        '<button type="button" class="close" data-dismiss="alert">×</button>' +
                         '<div>' + errortext + '</div>' +
                        '</div>';
        $('#provider-error').html(alerthtml);
    }

    function validform() {
        var name = $('#provider-form').find('[name=name]').val();

        if (name.length <= 0) {
            displayerror("Name is required.")
            return false;
        }
        if (id == '0') {
            var code = $('#provider-form').find('[name=code]').val();
            if (code.length != 3) {
                displayerror("Code length 3.")
                return false;
            }
        }
        return true;
    }
    var result = '@result';
    if (result != null && result.toString().length > 0) {
        displayerror(result.toString());
    }
</script>
